import { AssetManager, SpriteAtlas, SpriteFrame, TextAsset } from "cc";
import $planet_GlobalDC_poof$ from "../../../frame/common/data.global.center";

export default class $planet_GameGameAssets_poof$ {
	public static $planet_bundles_poof$: {
		$planet_resources_poof$: AssetManager.Bundle;
		$planet_language_poof$: AssetManager.Bundle;
	}

	private static $planet_lang_smap_poof$: Map<string, SpriteFrame> = new Map();
	private static $planet_lang_tmap_poof$: Map<string, string> = new Map();

	public static async $planet_init_poof$(){
		$planet_GameGameAssets_poof$.$planet_bundles_poof$ = {
			$planet_language_poof$: null,
			$planet_resources_poof$: null,
		};

		$planet_GameGameAssets_poof$.$planet_bundles_poof$.$planet_language_poof$ = await abd.$planet_cc_poof$.$planet_loadBundle_poof$('lang-en');

		await Promise.all((<const>[
			abd.$planet_cc_poof$ .$planet_loadResAsync_poof$(
				'texture', SpriteAtlas,
				$planet_GameGameAssets_poof$.$planet_bundles_poof$.$planet_language_poof$
			),abd.$planet_cc_poof$.$planet_loadResAsync_poof$(
				'text', TextAsset,
				$planet_GameGameAssets_poof$.$planet_bundles_poof$.$planet_language_poof$
			)
		])).then(([lang_tex, lang_txt])=>{
			lang_tex.getSpriteFrames().forEach(sframe=>$planet_GameGameAssets_poof$.$planet_lang_smap_poof$.set(sframe.name, sframe));
			let text = abd.$planet_parseJson_poof$(lang_txt.text.$planet_decodeAtoB_poof$);
			Reflect.ownKeys(text).forEach(key=>$planet_GameGameAssets_poof$.$planet_lang_tmap_poof$.set(key.toString(), Reflect.get(text, key)));
			return void 0;
		});

		$planet_GlobalDC_poof$.$planet_trigger_poof$.$planet_LOADED_poof$ = true;
	}

	/** 从当前的语言包中获取指定的贴图资源 */
	public static $planet_getLangTexture_poof$(texture: number|string): SpriteFrame{
		texture = $planet_getKey_poof$(texture, $planet_TEXTURE_LIST_poof$);
		return $planet_GameGameAssets_poof$.$planet_lang_smap_poof$.get(texture);
	}

	/** 从当前的语言包中获取指定的文本配置 */
	public static $planet_getLangText_poof$(text: number|string, ...args: any[]): string{
		text = $planet_getKey_poof$(text, $planet_TEXT_LIST_poof$);
		let str = $planet_GameGameAssets_poof$.$planet_lang_tmap_poof$.get(text).replace('\\n', '\n');
		if(str && args.length>0){
			args.forEach(($, i)=>str = str.replace(`{${i+1}}`, String($)));
		}
		return str;
	}
}

/**
 * 获取指定的索引或键
 * @param key
 * @param list
 */
function $planet_getKey_poof$(key: number|string, list: string[]){
	if(typeof key == 'string') key = abd.$planet_string_poof$.$planet_findLikeStr_poof$(key, list);
	else key = key.$planet_inRange_poof$(0, list.length-1) ? list[key] : list[0];
	return key;
}

const $planet_TEXTURE_LIST_poof$ = [
    "d29yZC1idXR0b24tYWdyZWVtZW50",
    "d29yZC1idXR0b24tcHJpdmFjeQ==",
    "d29yZC1idXR0b24tZnJlZQ==",
    "d29yZC1idXR0b24tY29udmVydA==",
    "d29yZC1idXR0b24tcmVhcA==",
    "ZmxvYXQtZ3VpZGU=",
    "ZmxvYXQtcmVzZXQ=",
    "YnV0dG9uLWhvbWUtZnJlZQ==",
    "YnV0dG9uLWhvbWUtY29udmVydA==",
    "d29yZC1idXR0b24tZ2lmdA==",
    "d29yZC1wb3AtdGl0bGUtY29uZ3JhdHVsYXRpb24=",
    "d29yZC1wb3AtdGl0bGUtc2V0dGluZw=="
].map($=>$.$planet_decodeAtoB_poof$);

const $planet_TEXT_LIST_poof$ = [
    "d29yZDE=",
    "d29yZDI=",
    "d29yZDM=",
    "d29yZDQ=",
    "d29yZDU=",
    "d29yZDY=",
    "d29yZDc=",
    "d29yZDg=",
    "d29yZDk=",
    "d29yZDEw",
    "d29yZDEx",
    "d29yZDEy",
    "d29yZDEz",
    "d29yZDE0",
    "d29yZDE1",
    "d29yZDE2",
    "d29yZDE3",
    "d29yZDE4",
    "d29yZDE5",
    "d29yZDIw",
    "d29yZDIx",
    "d29yZDIy",
    "d29yZDIz",
    "d29yZDI0",
    "d29yZDI1"
].map($=>$.$planet_decodeAtoB_poof$);
